package jh.com.jhappbf.platenumber;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import jh.com.jhappbf.R;

public class PlateNumberSelect extends FragmentActivity implements OnClickListener {
    private ViewPager mPager;
    private ImageView mBackImg;
    private ImageView mRefreshImg;
    private TextView mPlatenumTxt;
    private Button mBtnSave;
    private RadioButton mRadioArea;
    private RadioButton mRadioCharacter;
    private RadioButton mRadioDigit;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.platenum_activity);
        findView();
        initView();
        if (arg0 != null) {
            String value = arg0.getString("state");
            mPlatenumTxt.setText(value);
        }
    }

    /**
     * 查找控件
     */
    private void findView() {
        mPager = findViewById(R.id.platenum_viewPager);
        mBackImg = findViewById(R.id.platenum_title_comeback_img);
        mRefreshImg = findViewById(R.id.platenum_title_save_img);
        mPlatenumTxt = findViewById(R.id.platenum_content_txt);
        mRadioArea = findViewById(R.id.platenum_abbreviaction);
        mRadioArea.setTag(0);
        mRadioCharacter = findViewById(R.id.platenum_character);
        mRadioCharacter.setTag(1);
        mRadioDigit = findViewById(R.id.platenum_digit);
        mRadioDigit.setTag(2);
        mBtnSave = findViewById(R.id.platenum_confirm_btn);
    }

    /**
     * 初始化
     */
    private void initView() {
        FragmentManager fm = getSupportFragmentManager();
        mPager.setAdapter(new myFragmentPageAdapter(fm));
        mBackImg.setOnClickListener(this);
        mRefreshImg.setOnClickListener(this);
        mBtnSave.setOnClickListener(this);
        mRadioArea.setOnClickListener(this);
        mRadioCharacter.setOnClickListener(this);
        mRadioDigit.setOnClickListener(this);
        mPager.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int index) {
                int value = index % 3;
                if (value == 0) {
                    mRadioArea.setChecked(true);
                    mRadioArea.setTag(index);
                } else if (value == 1) {
                    mRadioCharacter.setChecked(true);
                    mRadioCharacter.setTag(index);
                } else if (value == 2) {
                    mRadioDigit.setChecked(true);
                    mRadioDigit.setTag(index);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int index) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.platenum_title_comeback_img:
                this.finish();
                break;
            case R.id.platenum_title_save_img:
                confirmSelect();
                break;
            case R.id.platenum_confirm_btn:
                removeLastCharacter();
                break;
            case R.id.platenum_abbreviaction:
                int index = (Integer) mRadioArea.getTag();
                mPager.setCurrentItem(index);
                break;
            case R.id.platenum_character:
                int index1 = (Integer) mRadioCharacter.getTag();
                mPager.setCurrentItem(index1);
                break;
            case R.id.platenum_digit:
                int index2 = (Integer) mRadioDigit.getTag();
                mPager.setCurrentItem(index2);
                break;
            default:
                break;
        }
    }

    /**
     * 保存
     */
    private void confirmSelect() {
        Intent i = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("result", mPlatenumTxt.getText().toString());
        i.putExtras(bundle);
        setResult(3, i);
        this.finish();
    }

    /**
     * 删除最后一个字符
     */
    private void removeLastCharacter() {
        String content = mPlatenumTxt.getText().toString();
        if (content.length() > 0) {
            content = content.substring(0, content.length() - 1);
            mPlatenumTxt.setText(content);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("state", mPlatenumTxt.getText().toString());
    }

    class myFragmentPageAdapter extends FragmentPagerAdapter {
        myFragmentPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int index) {
            Fragment fragment;
            index = index % 3;
            if (index == 0) {
                fragment = new AreaFragment();
            } else if (index == 1) {
                fragment = new CharacterFragment();
            } else {
                fragment = new DigitFragment();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 1000;
        }
    }
}
